using System;
using System.Collections.Generic;
using System.Text;
using MouseGestures;
using MouseGesturesAddIn;
using System.Collections.Specialized;
using EnvDTE80;
using EnvDTE;

namespace MouseGesturesAddIn {
  /// <summary>
  /// Class that maintains configuration of the add-in
  /// </summary>
  public class Configuration {
    private DTE2 application;

    /// <summary>
    /// Internal storage for configuration
    /// </summary>
    private NameValueCollection actions;

    /// <summary>
    /// Gets or sets ActionName for specified mouse gesture
    /// </summary>
    /// <param name="gesture">The gesture we are working with.</param>
    public string this[MouseGesture gesture] {
      get {
        if ( gesture == MouseGesture.Unknown )
          return null;
        string name = Enum.GetName(typeof(MouseGesture), ( MouseGesture )gesture);
        return actions[name];
      }
      set {
        if ( gesture == MouseGesture.Unknown )
          return;
        string name = Enum.GetName(typeof(MouseGesture), ( MouseGesture )gesture);
        actions[name] = value;
      }
    }

    /// <summary>
    /// Gets or sets ActionName for specified mouse gesture
    /// </summary>
    /// <param name="gesture">The name of the gesture we are working with.</param>
    public string this[string gestureName] {
      get {
        if ( gestureName == "Unknown" )
          return null;
        return actions[gestureName];
      }
      set {
        if ( gestureName == "Unknown" )
          return;
        actions[gestureName] = value;
      }
    }

    /// <summary>
    /// Creates new instance of configuration object and loads configuration data.
    /// </summary>
    /// <param name="aApplication">DTE2 application object</param>
    /// <remarks>Configuration data are stored in DTE2.Glogals object (Visual Studio will maintain these setting for us)</remarks>
    public Configuration(DTE2 aApplication) {
      application = aApplication;
      actions = new NameValueCollection();

      Globals globals = application.Globals;


      foreach(string gesture in Enum.GetNames(typeof(MouseGesture))) {
        if ( gesture == "Unknown" )
          continue;

        //Full name of settings in the DTE2.Glogals object is "MouseGestures_" + gesture
        string gestureAction = "";
        string fullName = "MouseGestures_" + gesture;

        if ( globals.get_VariableExists(fullName) ) {
          //if exists -> load
          gestureAction = (string)globals[fullName];
        }
        else {
          //creates new
          globals[fullName] = "";
          globals.set_VariablePersists(fullName, true);
        }

        actions.Add(gesture, gestureAction);
      }
    }

    /// <summary>
    /// Saves setting.
    /// </summary>
    public void Save() {
      Globals globals = application.Globals;

      foreach ( string gesture in Enum.GetNames(typeof(MouseGesture)) ) {
        if ( gesture == "Unknown" )
          continue;

        globals["MouseGestures_" + gesture] = actions[gesture];
      }
    }

  }
}
